<template>
  <el-drawer v-model="showDrawer" :title="title" :size="size" :close-on-click-modal="false"
    :destroy-on-close="destroyOnClose">
    <div class="formDrawer">
      <div class="body">
        <slot></slot>
      </div>
      <div class="actions">
        <el-button type="primary" @click="onSubmit" :loading="confirmLoading">{{ confirmText }}</el-button>
        <el-button type="default" @click="close">取消</el-button>
      </div>
    </div>
  </el-drawer>
</template>
<script lang="ts">
import { ref, defineComponent } from "vue"

export default defineComponent({
  props: {
    title: String,
    size: {
      type: String,
      default: "45%"
    },
    destroyOnClose: {
      type: Boolean,
      default: true
    },
    confirmText: {
      type: String,
      default: "提交"
    }
  },
  emits: ['submit'],
  setup(_, { expose, emit }) {
    const showDrawer = ref(false)

    const confirmLoading = ref(false)
    const showLoading = () => confirmLoading.value = true
    const hideLoading = () => confirmLoading.value = false

    // 打开
    const open = () => showDrawer.value = true

    // 关闭
    const close = () => showDrawer.value = false


    // 提交
    const onSubmit = () => {
      emit('submit')
    }

    // 向父组件暴露以下方法
    expose({
      open: () => showDrawer.value = true,
      close: () => showDrawer.value = false,
      showLoading: () => confirmLoading.value = true,
      hideLoading: () => confirmLoading.value = false
    })
    return {
      showDrawer,
      confirmLoading,
      open,
      close,
      onSubmit,
      showLoading,
      hideLoading
    }
  }
})

</script>
<style scoped>
.formDrawer {
  width: 100%;
  height: 100%;
  position: relative;
  @apply flex flex-col;
}

.formDrawer .body {
  flex: 1;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 50px;
  overflow-y: auto;
}

.formDrawer .actions {
  height: 50px;
  @apply mt-auto flex items-center;
}
</style>